/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.arquimedes.jms;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.naming.Context;
import javax.naming.InitialContext;

/**
 *
 * @author Juan David Ramírez
 */
public class Receptor extends Thread{

    public void recibir() throws Exception {
	// Gets the JNDI context
	Context jndiContext = new InitialContext();
	// Looks up the administered objects
	ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup("Prueba");
	Topic topic = (Topic) jndiContext.lookup("jms/Prueba");
	// Creates the needed artifacts to connect to the queue
	Connection connection = connectionFactory.createConnection();
	Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
	MessageConsumer consumer = session.createConsumer(topic);
	connection.start();
	// Loops to receive the messages
	while (true) {
	    TextMessage message = (TextMessage) consumer.receive();
	    System.out.println("Message received: " + message.getText());
	}
    }

    @Override
    public void run() {
	try {
	    recibir();
	} catch (Exception ex) {
	    Logger.getLogger(Receptor.class.getName()).log(Level.SEVERE, null, ex);
	}
    }
    
    
}
